package ibms_rcp.dao;

import ibms_rcp.constants.Constants;
import ibms_rcp.db.DataBaseManager;
import ibms_rcp.model.RechargeLog;
import ibms_rcp.util.SqlUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;

public class RechargeManager {
	
	//充值，并且调用saveRechargeLog填写记录
	//业务逻辑如下：首先获取预存款，然后更新，更新后再次检查预存款是否已更新
	public static void saveRecharge(Long company_moi_id,String company_name,double value) {
		//连接数据库
		Connection conn = DataBaseManager.getConnection();
		try {
			//禁止自动提交
			conn.setAutoCommit(false);
			double oldprice = 0;
			double newprice = 0;
			//获得预存款额
			String sqlString = "select new_value from m2mnap_moi_attribute"+
					" where moi_id = ? and moc_attribute_code=?";
			PreparedStatement pstmt = conn.prepareStatement(sqlString);
			pstmt.setLong(1,company_moi_id);
			pstmt.setString(2, "bookSaving");
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				oldprice = rs.getDouble(1);
			}
			newprice = oldprice+value;
			
			//更新指定moi_id的company的预存款
			sqlString = "update m2mnap_moi_attribute"+
			" set old_value = new_value , new_value = ? where moi_id = ? and moc_attribute_code=?";
			pstmt = conn.prepareStatement(sqlString);
			pstmt.setDouble(1, newprice);
			pstmt.setLong(2,company_moi_id);
			pstmt.setString(3, "bookSaving");
			pstmt.executeUpdate();
			
			//再次读取预存款，查看是否添加成功
			sqlString = "select new_value from m2mnap_moi_attribute"+
					" where moi_id = ? and moc_attribute_code=?";
			pstmt = conn.prepareStatement(sqlString);
			pstmt.setLong(1,company_moi_id);
			pstmt.setString(2, "bookSaving");
			rs = pstmt.executeQuery();
			if(rs.next()){
				if(newprice == rs.getDouble(1)){
					//若更新预存款成功，则添加充值记录
					Long user_id = 111L;
					String user_name = "小李";
					System.out.println("充值成功，公司名称:"+company_name+";充值金额："+value+
							";充后金额："+newprice+";操作员姓名："+user_name);
					saveRechargeLog(company_moi_id,company_name,value,newprice,user_id,user_name);
					
					//充值成功后，自动将账单中未付的账单付账
					PayManager.saveAutoPayLogbyCompany_moi_id(company_moi_id);
				}
				else{
					;
				}
			}
			pstmt.close();
			conn.commit();
		} catch (SQLException e) {
			try {
				//回滚
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		System.out.println("RechargeManager saveRecharge() called.");
	}

	//添加充值记录表
	public static void saveRechargeLog(Long company_moi_id, String company_name,
			double value, double newprice, Long user_id, String user_name) {
		// TODO Auto-generated method stub
		Connection conn = DataBaseManager.getConnection();
		try {
			//禁止自动提交
			conn.setAutoCommit(false);
			//添加充值记录
			String sqlString = "insert into m2mnap_estate_recharge_log " +
					"(id,user_id,user_name,moi_id,moi_name,recharge_time,recharge_point,point)" +
					" values(nextval('m2mnap_moi_attribute_seq'),?,?,?,?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(sqlString);
			pstmt.setLong(1,user_id);
			pstmt.setString(2, user_name);
			pstmt.setLong(3, company_moi_id);
			pstmt.setString(4, company_name);
			//获取当期系统的时间戳以供使用
			pstmt.setTimestamp(5, SqlUtil.getTimeStamp());
			pstmt.setDouble(6, value);
			pstmt.setDouble(7, newprice);
			
			int lines = pstmt.executeUpdate();
			
			if(lines>0){
				System.out.println("添加记录成功");
				
			}else{
				;
			}
		} catch (SQLException e) {
			try {
				//回滚
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}		
		System.out.println("CompanyManager saveRechargeLog() called.");

	}
	
	//查询指定公司，指定日期内的充值记录
	public static ArrayList<RechargeLog> getRechargeLogs(Long company_id,String beginDate,String endDate){
		
		ArrayList<RechargeLog> logs = new ArrayList<RechargeLog>();
		
		Connection conn = DataBaseManager.getConnection();
		try {
			//禁止自动提交
			conn.setAutoCommit(false);
			//查询充值记录
			String sqlString = "select * from m2mnap_estate_recharge_log " +
					"where moi_id = ? and recharge_time > ? and recharge_time < ? ";
			PreparedStatement pstmt = conn.prepareStatement(sqlString);
			pstmt.setLong(1,company_id);
			pstmt.setTimestamp(2, java.sql.Timestamp.valueOf(beginDate+" 00:00:00.0"));
			pstmt.setTimestamp(3, java.sql.Timestamp.valueOf(endDate+" 00:00:00.0"));
			
			/*SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
		    String time = df.format(new Date());   
		    Timestamp ts = Timestamp.valueOf(time);
		       
			pstmt.setString(2, 2010-1-1);
			pstmt.setString(3, 2012-12-12);
			pstmt.set*/
			
			ResultSet rs = pstmt.executeQuery();
			//RechargeLog rLog = new RechargeLog();
			while(rs.next()){
				RechargeLog rLog = new RechargeLog();
				rLog.setId(rs.getLong(1));//充值记录id
				System.out.println(rs.getLong(1));
				rLog.setUser_id(rs.getLong(2));//user_id
				rLog.setUser_name(rs.getString(3));//user_name
				rLog.setCompany_id(rs.getLong(4));//company_id
				rLog.setCompany_name(rs.getString(5));//company_name
				rLog.setRecharge_time(rs.getTimestamp(6));//recharge_timestamp
				rLog.setRecharge_point(rs.getDouble(7));//recharge_point
				rLog.setPoint(rs.getDouble(8));//point;
				
				if(rLog != null){
					logs.add(rLog);
				}
			}
			System.out.println(logs.size());
			conn.commit();

		} catch (SQLException e) {
			try {
				//回滚
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}		
		System.out.println("CompanyManager getRechargeLogs() called.");
		
		return logs;
		
	}
	
	public static void main(String [] args){
		String mString = "2010-10-10";
		//DateFormat();
		//saveRecharge(4839L, "联想", 125);
		RechargeManager.getRechargeLogs(4839L,"2012-10-9","2012-10-12");
		System.out.println();
	}
}
